#version 450 compatibility



layout(local_size_x = DEF_LocalSize, local_size_y = DEF_LocalSize, local_size_z = 1) in;

layout( binding = 0, rgba8ui) uniform uimage2D Pix;
layout( location = 1) uniform sampler2D Image;

layout( binding = 2 ) uniform Buf_LoalParameter0 {
    ivec4   m_Box;
    ivec2   m_Size;
    ivec2   m_Offset;
    uint    m_MixedMode;
};


void main(){
    vec2 coord = textureSize(Image, 0);
    ivec2 Wcoord = ivec2(gl_WorkGroupID.x, gl_WorkGroupID.y) + m_Offset;

    if(m_Offset.x < m_Box.x) Wcoord.x += (m_Box.x - m_Offset.x);
    if(m_Offset.y < m_Box.y) Wcoord.y += (m_Box.y - m_Offset.y);

    if(Wcoord.x >= m_Box.x && Wcoord.y >= m_Box.y && Wcoord.x < m_Box.z && Wcoord.y < m_Box.w){
        coord.x = float(gl_WorkGroupID.x) / m_Size.x;
        coord.y = float(gl_WorkGroupID.y) / m_Size.y;
        
        vec4 color = texture(Image, coord);

        uvec4 c = imageLoad(Pix, Wcoord);
        
        c.r = uint(float(c.r)*(1-color.a) + (color.r*color.a*255));
        c.g = uint(float(c.g)*(1-color.a) + (color.g*color.a*255));
        c.b = uint(float(c.b)*(1-color.a) + (color.b*color.a*255));

        imageStore(Pix, Wcoord, c);
    }   
        

}




